<?php 
class ControllerAccountInvoice extends Controller {
	public function index() { 
    	if (!$this->customer->isLogged()) {
			if (isset($this->request->get['order_id'])) {
				$order_id = $this->request->get['order_id'];
			} else {
				$order_id = 0;
			}	
			
			$this->session->data['redirect'] = HTTPS_SERVER . 'index.php?route=account/invoice&order_id=' . $order_id;
			
			$this->redirect(HTTPS_SERVER . 'index.php?route=account/login');
    	}
	  
    	$this->language->load('account/invoice');

    	$this->document->title = $this->language->get('heading_title');

		if (isset($this->request->get['order_id'])) {
			$order_id = $this->request->get['order_id'];
		} else {
			$order_id = 0;
		}
      	
		$this->document->breadcrumbs = array();

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTP_SERVER . 'index.php?route=common/home',
        	'text'      => $this->language->get('text_home'),
        	'separator' => FALSE
      	); 

      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/account',
        	'text'      => $this->language->get('text_account'),
        	'separator' => $this->language->get('text_separator')
      	);
		
      	$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/history',
        	'text'      => $this->language->get('text_history'),
        	'separator' => $this->language->get('text_separator')
      	);
      	
		$this->document->breadcrumbs[] = array(
        	'href'      => HTTPS_SERVER . 'index.php?route=account/invoice&order_id=' . $order_id,
        	'text'      => $this->language->get('text_invoice'),
        	'separator' => $this->language->get('text_separator')
      	);
		
		$this->load->model('account/order');

		if (isset($this->request->get['order_id'])) {
			$order_id = $this->request->get['order_id'];
		} else {
			$order_id = 0;
		}
			
		$order_info = $this->model_account_order->getOrder($order_id);
		
		if ($order_info) {
      		$this->data['heading_title'] = $this->language->get('heading_title');
			
			$this->data['text_invoice_id'] = $this->language->get('text_invoice_id');
    		$this->data['text_order_id'] = $this->language->get('text_order_id');
			$this->data['text_email'] = $this->language->get('text_email');
			$this->data['text_telephone'] = $this->language->get('text_telephone');
			$this->data['text_fax'] = $this->language->get('text_fax');
      		$this->data['text_shipping_address'] = $this->language->get('text_shipping_address');
      		$this->data['text_shipping_method'] = $this->language->get('text_shipping_method');
      		$this->data['text_payment_address'] = $this->language->get('text_payment_address');
      		$this->data['text_payment_method'] = $this->language->get('text_payment_method');
      		$this->data['text_order_history'] = $this->language->get('text_order_history');
      		$this->data['text_product'] = $this->language->get('text_product');
      		$this->data['text_model'] = $this->language->get('text_model');
      		$this->data['text_quantity'] = $this->language->get('text_quantity');
      		$this->data['text_price'] = $this->language->get('text_price');
      		$this->data['text_total'] = $this->language->get('text_total');
			$this->data['text_comment'] = $this->language->get('text_comment');

			$this->data['column_date_added'] = $this->language->get('column_date_added');
			$this->data['column_status'] = $this->language->get('column_status');
			$this->data['column_comment'] = $this->language->get('column_comment');

			$this->data['text_invoice_attachments'] = $this->language->get('text_invoice_attachments');
			$this->data['text_licenses'] = $this->language->get('text_licenses');
			$this->data['text_contract'] = $this->language->get('text_contract');
			$this->data['text_product_contract'] = $this->language->get('text_product_contract');
			$this->data['text_upload_contract'] = $this->language->get('text_upload_contract');

			$this->data['entry_filename'] = $this->language->get('entry_filename');

			$this->data['button_save'] = $this->language->get('button_save');
			$this->data['button_continue'] = $this->language->get('button_continue');
			$this->data['button_download'] = $this->language->get('button_download');
			
			$this->data['contract_link'] = $this->url->https('checkout/confirm/dogovorPdf');
			$this->data['product_contract_link'] = $this->url->https('checkout/confirm/dogovorProductPdf');

			$this->data['order_id'] = $this->request->get['order_id'];
			$this->data['email'] = $order_info['email'];
			$this->data['telephone'] = $order_info['telephone'];
			$this->data['fax'] = $order_info['fax'];
			$this->data['button_otpravit_dogovor'] = $this->language->get('button_otpravit_dogovor');

			$url = '';

			if (isset($order_info['order_id'])) {
				$url .= '&order=' . $order_info['order_id'];
			}
			if (!isset($this->request->get['download_id'])) {
				$this->data['action'] = $this->url->https('account/invoice/insert' . $url);
			} else {
				$this->data['action'] = $this->url->https('account/invoice/update&download_id=' . $this->request->get['download_id'] . $url);
			}

			if (isset($this->error['download'])) {
				$this->data['error_download'] = $this->error['download'];
			} else {
				$this->data['error_download'] = '';
			}

			if ($order_info['shipping_address_format']) {
				$format = $order_info['shipping_address_format'];
			} else {
				$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
			}

			$find = array(
	  			'{firstname}',
	  			'{lastname}',
	  			'{company}',
      			'{address_1}',
      			'{address_2}',
     			'{city}',
      			'{postcode}',
      			'{zone}',
				'{zone_code}',
      			'{country}'
      			);

      			$replace = array(
	  			'firstname' => $order_info['shipping_firstname'],
	  			'lastname'  => $order_info['shipping_lastname'],
	  			'company'   => $order_info['shipping_company'],
      			'address_1' => $order_info['shipping_address_1'],
      			'address_2' => $order_info['shipping_address_2'],
      			'city'      => $order_info['shipping_city'],
      			'postcode'  => $order_info['shipping_postcode'],
      			'zone'      => $order_info['shipping_zone'],
				'zone_code' => $order_info['shipping_zone_code'],
      			'country'   => $order_info['shipping_country']  
			);

			$this->data['shipping_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

			$this->data['shipping_method'] = $order_info['shipping_method'];

			if ($order_info['payment_address_format']) {
      			$format = $order_info['payment_address_format'];
    		} else {
				$format = '{firstname} {lastname}' . "\n" . '{company}' . "\n" . '{address_1}' . "\n" . '{address_2}' . "\n" . '{city} {postcode}' . "\n" . '{zone}' . "\n" . '{country}';
			}
		
    		$find = array(
	  			'{firstname}',
	  			'{lastname}',
	  			'{company}',
      			'{address_1}',
      			'{address_2}',
     			'{city}',
      			'{postcode}',
      			'{zone}',
				'{zone_code}',
      			'{country}'
			);
	
			$replace = array(
	  			'firstname' => $order_info['payment_firstname'],
	  			'lastname'  => $order_info['payment_lastname'],
	  			'company'   => $order_info['payment_company'],
      			'address_1' => $order_info['payment_address_1'],
      			'address_2' => $order_info['payment_address_2'],
      			'city'      => $order_info['payment_city'],
      			'postcode'  => $order_info['payment_postcode'],
      			'zone'      => $order_info['payment_zone'],
				'zone_code' => $order_info['payment_zone_code'],
      			'country'   => $order_info['payment_country']  
      			);

      			$this->data['payment_address'] = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', trim(str_replace($find, $replace, $format))));

      			$this->data['payment_method'] = $order_info['payment_method'];

      			$this->data['products'] = array();

      			$products = $this->model_account_order->getOrderProducts($this->request->get['order_id']);

      			$product_id = '1';

      			foreach ($products as $product) {
      				$options = $this->model_account_order->getOrderOptions($this->request->get['order_id'], $product['order_product_id']);

      				$product_id = $product['product_id'];

        		$option_data = array();

        		foreach ($options as $option) {
        			$option_data[] = array(
            			'name'  => $option['name'],
            			'value' => $option['value'],
        			);
        		}

        		$this->data['products'][] = array(
          			'name'     => $product['name'],
          			'model'    => $product['model'],
          			'option'   => $option_data,
          			'quantity' => $product['quantity'],
          			'price'    => $this->currency->format($product['price'], $order_info['currency'], $order_info['value']),
					'total'    => $this->currency->format($product['total'], $order_info['currency'], $order_info['value'])
        		);
      			}

      			$this->load->model('catalog/product');
      			 
      			$this->data['licenses_images'] = array();

      			$results = $this->model_catalog_product->getProductLicensesImages($product_id);

      			$this->load->model('tool/image');

      			if ($results[0]['image_l']) {
      				$image = $results['image_l'];
      			} else {
      				$image = 'no_image.jpg';
      			}

      			$this->data['popup'] = $this->model_tool_image->resize($image, $this->config->get('config_image_popup_width'), $this->config->get('config_image_popup_height'));
      			$this->data['thumb'] = $this->model_tool_image->resize($image, $this->config->get('config_image_thumb_width'), $this->config->get('config_image_thumb_height'));
      			 

      			$this->data['totals'] = $this->model_account_order->getOrderTotals($this->request->get['order_id']);

      			$this->data['comment'] = $order_info['comment'];

      			$this->data['historys'] = array();

      			$results = $this->model_account_order->getOrderHistorys($this->request->get['order_id']);

      			foreach ($results as $result) {
        		$this->data['historys'][] = array(
          			'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
          			'status'     => $result['status'],
          			'comment'    => nl2br($result['comment'])
        		);
      			}

      		$this->data['continue'] = HTTPS_SERVER . 'index.php?route=account/history';

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/invoice.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/account/invoice.tpl';
			} else {
				$this->template = 'default/template/account/invoice.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/footer',
				'common/column_left',
				'common/header'
			);		
			
			$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));		
    	} else {
      		$this->data['heading_title'] = $this->language->get('heading_title');

      		$this->data['text_error'] = $this->language->get('text_error');

      		$this->data['button_continue'] = $this->language->get('button_continue');

      		$this->data['continue'] = HTTP_SERVER . 'index.php?route=account/history';
      			
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$this->template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$this->template = 'default/template/error/not_found.tpl';
			}
			
			$this->children = array(
				'common/column_right',
				'common/footer',
				'common/column_left',
				'common/header'
				);
					
				$this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
		}
	}

	public function insert() {
		$this->load->language('account/invoice');
		$this->document->title = $this->language->get('heading_title');


			
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$data = array();

			if (is_uploaded_file($this->request->files['download']['tmp_name'])) {
				$filename = $this->request->files['download']['name'] . '.' . md5(rand());

				move_uploaded_file($this->request->files['download']['tmp_name'], DIR_DOWNLOAD . $filename);

				if (file_exists(DIR_DOWNLOAD . $filename)) {
					$data['download'] = $filename;
					$data['mask'] = $this->request->files['download']['name'];
				}
			}
			//			$data['download_description'] = array();
			$order_id =  $this->request->get['order'];
			$data['products'] = array();
			$this->load->model('account/order');
			$products = $this->model_account_order->getOrderProducts($order_id);

			$data['products'] = $products;
			$data['remaining'] = 5;
			$data['order_id'] = $order_id;
			$this->load->model('account/download');
			$this->model_account_download->addDownload(array_merge($this->request->post, $data));

			$this->session->data['success'] = $this->language->get('text_success');

			$this->redirect($this->url->https('account/history'));
		}

		$this->index();
	}

	private function validateForm() {

		if ($this->request->files['download']['name']) {
			if ((strlen(utf8_decode($this->request->files['download']['name'])) < 3) || (strlen(utf8_decode($this->request->files['download']['name'])) > 128)) {
				$this->error['download'] = $this->language->get('error_filename');
			}

			if (substr(strrchr($this->request->files['download']['name'], '.'), 1) == 'php') {
				$this->error['download'] = $this->language->get('error_filetype');
			}

			if ($this->request->files['download']['error'] != UPLOAD_ERR_OK) {
				$this->error['warning'] = $this->language->get('error_upload_' . $this->request->files['download']['error']);
			}
		}

		if (!$this->error) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	public function dogovor() {
		if (true) {
			$mypdf = PDF_new();
			PDF_open_file($mypdf, "");
			PDF_begin_page($mypdf, 595, 842);
			$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);
			PDF_setfont($mypdf, $myfont, 10);

			PDF_show_xy($mypdf, 'Contract ..........', 80, 800);

			PDF_end_page($mypdf);
			PDF_close($mypdf);
			$mybuf = PDF_get_buffer($mypdf);
			$mylen = strlen($mybuf);
			$this->response->addheader('Content-type', 'application/pdf');
			$this->response->addheader('Content-Length', '$mylen');
			$this->response->addheader('Content-Disposition', 'inline; filename=dogovorsd.pdf');
			$this->response->setOutput($mybuf);
			PDF_delete($mypdf);
		} else {
			return $this->forward('error/error_404', 'index');
		}
	}
}
?>